/*
 * @description: 
 * @Author: latiao
 * @Date: 2022-11-23 15:18:47
 * @LastEditors: latiao
 * @LastEditTime: 2022-11-23 15:22:14
 */
const fs = require("fs");
const path = require("path");

function removeDir(dir) {
  let files = fs.readdirSync(dir);
  for (var i = 0; i < files.length; i++) {
    let newPath = path.join(dir, files[i]);
    let stat = fs.statSync(newPath);
    if (stat.isDirectory()) {
      //如果是文件夹就递归下去
      removeDir(newPath);
    } else {
      //删除文件
      fs.unlinkSync(newPath);
    }
  }
  fs.rmdirSync(dir); //如果文件夹是空的，就将自己删除掉
}

const removeLidt = () => {
  const indexPath = path.resolve(__dirname, "../dist/build/mp-weixin/pages/index/index.json");
  const filePath = path.resolve(__dirname, "../dist/build/mp-weixin/app.json");
  const homePath = path.resolve(
    __dirname,
    "../dist/build/mp-weixin/pages/index/modules/home/home.json",
  );

  const data = JSON.parse(fs.readFileSync(filePath).toString());
  const indexData = JSON.parse(fs.readFileSync(indexPath).toString());
  const homeData = JSON.parse(fs.readFileSync(homePath).toString());

  delete indexData.usingComponents.live;
  delete homeData.usingComponents["home-live"];

  delete data.plugins;
  const index = data.subPackages[1].pages.findIndex(page => page.includes("pages/live/live"));

  data.subPackages[1].pages.splice(index, 1);

  fs.writeFileSync(filePath, JSON.stringify(data));
  fs.writeFileSync(indexPath, JSON.stringify(indexData));
  fs.writeFileSync(homePath, JSON.stringify(homeData));

  removeDir(path.resolve(__dirname, "../dist/build/mp-weixin/subcontract/pages/live"));
  removeDir(path.resolve(__dirname, "../dist/build/mp-weixin/pages/index/modules/live"));
  removeDir(
    path.resolve(
      __dirname,
      "../dist/build/mp-weixin/pages/index/modules/home/components/home-live",
    ),
  );
  console.log('remove live')
};

removeLidt();
